package com.blank.gpstest;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.blank.net.MyObserver;
import com.blank.net.RestPool;
import com.blank.net.config.NetConfig;
import com.bumptech.glide.Glide;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class NetActivity extends AppCompatActivity {

    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_net);
        imageView = findViewById(R.id.iv);
        initNet();
        initData();
    }

    private void initData() {
        RestPool.getInstance().getService(Api.class)
                .getSTStoken()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MyObserver<ImageData>(this) {
                    /**
                     * 根据具体的Api 业务逻辑去重写 onSuccess 方法！Error 是选择重写，but 必须Super ！
                     *
                     * @param stringHttpResult
                     */
                    @SuppressLint("CheckResult")
                    @Override
                    public void onSuccess(ImageData stringHttpResult) {
                        String url = stringHttpResult.getData().get(0).getImg_1024_768();
                        Log.i("TAG", "onSuccess: " + url);
                        Glide.with(getApplication())
                                .load(url)
                                .into(imageView);
                    }

                    @Override
                    public void onFailure(boolean success, String message) {

                    }
                });


    }

    private void initNet() {
        NetConfig builder = new NetConfig.Builder()
                .setBaseUrl("http://wallpaper.apc.360.cn")
                .build();
        builder.initContext(this);
    }
}